package cn.tedu.front.details.service.impl;

import cn.tedu.common.cache.CityCacheConsts;
import cn.tedu.front.details.dao.cache.ICityCacheRepository;
import cn.tedu.front.details.dao.persest.repository.CityRepository;
import cn.tedu.front.details.pojo.vo.CityVO;
import cn.tedu.front.details.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityRepository cityRepository;

    @Autowired
    private ICityCacheRepository cityCacheRepository;

    @Override
    public List<CityVO> listByParent(Long parentId) {
//        List<CityVO> cityVOS = cityRepository.listByParent(parentId);
        List<CityVO> cityVOS = cityCacheRepository.listByParent(parentId);
        return cityVOS;
    }

    @Override
    public void rebuildCache() {
        Long parentId = 0L;
        List<CityVO> cityVOS = cityRepository.listByParent(parentId);
        cityCacheRepository.saveList(parentId,cityVOS);
        callRecursion(cityVOS);
    }

    public void callRecursion(List<CityVO> cityVOS){
        for (CityVO cityVO : cityVOS) {
            Long id = cityVO.getId();
            List<CityVO> subList = cityRepository.listByParent(id);
            if (subList.size() > 0){
                cityCacheRepository.saveList(id,subList);
                callRecursion(subList);
            }
        }
    }
}
